/**
 * jqGrid 封装类
 *
 * @author lzg128ty@163.COM
 * @version 2014-8-22
 */
var DataGrid = function(options) {
    $.jgrid.defaults.styleUI = "Bootstrap";

    var dataGrid = options.dataGrid ? options.dataGrid : $("#dataGrid");
    var searchForm = options.searchForm ? options.searchForm : $("#searchForm");
    var dataGridPage = options.dataGridPage ? options.dataGridPage : $("#dataGridPage");
    var _rowNum = options.rowNum ? options.rowNum : 20;
    var _rowList = options.rowList ? options.rowList : [10,20,50,100];

    options.colNames = [];
    options.shrinkToFit = false;
    var modelWidth = null;
    var bodyWidth = 0;
    var bodyHeight = 0;
    getBodySize();
    for (var i = 0; i < options.colModel.length; i++) {
        options.colNames.push(options.colModel[i].header);
        modelWidth += options.colModel[i].width;
    }
    if (modelWidth < bodyWidth) {
        options.shrinkToFit = true;
    }
    // 设置列模型
    options = $.extend({
        url : searchForm.attr('action'),
        postData : searchForm.serializeObject(),
        pager : dataGridPage,
        mtype : "POST",
        datatype : "json",
        //altRows : true, // 斑马线样式，交替行altclass
        autowidth : true,
        shrinkToFit : options.shrinkToFit,
        colNames : [],
        colModel : options.columnModel,
        rowList: _rowList,
        rowNum : _rowNum,
        viewrecords : true,
        recordpos: "left",
        pagerpos: "right",
        loadComplete: function(xhr){
            if(!options.shrinkToFit){
                $(this).closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" })
            }

            if(!options.height){
                console.log(bodyHeight);
                console.log($('.toolbar').innerHeight()+'px');
                $(this).setGridHeight(bodyHeight - $('.toolbar').innerHeight()+'px');
            }
        },
        loadError : function(xhr,status,error){
            if(xhr.readyState==4){
                var responseText = xhr.responseText;
                if(responseText.indexOf("loginForm")>=0 && responseText.indexOf("登录")>=0){
                    window.location.reload(true);
                }
            }
           alert("登录失效，请重新登录");
        }
    },options);

    dataGrid.jqGrid(options);

    $(window).resize(function(){
        resizeDataGrid();
    });

    /**
     * 自动调整表格大小
     */
    function resizeDataGrid(){
        getBodySize();
        dataGrid.setGridWidth(bodyWidth);
        dataGrid.setGridHeight(bodyHeight- $('.jax-box').innerHeight()+'px');
        // setTableHeight()
    }

    //设置表格高度
    /*function setTableHeight() {
        if ($(".form-content").length) {
            var height = $('.layui-tab-content', parent.document).innerHeight() - $(".form-content").innerHeight() - $(".ui-jqgrid-hbox").innerHeight() - 35;
            if ($("#pager_list").length) {
                height = height - $("#pager_list").innerHeight();
            }
            // console.log(height);
            height && $(".ui-jqgrid-bdiv").css({
                "overflow": "auto",
                "height": height
            });
        }
    }
*/
    function getBodySize(){
        bodyWidth = $('.content').width() - 50;

        bodyHeight = $('.content-wrapper').height() - 250;
    }
};
